﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.IO;
using Common;

public partial class admin_finance_financerecordlist : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin("finance", 1);

        switch (RequestBase.GetFormString("otype"))
        {
            case "loaddata":
                LoadingData();
                break;
            case "delete":
                Delete();
                break;
        }

        if (!IsPostBack)
        {
            InitDateControl();
            CategoryDataBind();
        }
    }

    #region 下拉菜单
    void CategoryDataBind()
    {
        DataTable dat = BLL.Search.CommonSearch.ExecSelect("EnumDef",
             new string[] { "[Type]" },
             new string[] { "Equal" },
             new string[] { "'" + RequestBase.GetQueryString("typecode").Replace("1", "Income").Replace("2", "Outlay") + "'" },
             null,
             null);

        selCategoryList.Items.Add(new ListItem("-- Select --", ""));
        foreach (DataRow dr in dat.Select(""))
        {
            selCategoryList.Items.Add(new ListItem(dr["Title"].ToString().Trim(), dr["EnumKey"].ToString().Trim()));
        }
        dat.Dispose();

        selCategoryList.Attributes.Add("onchange", "LoadSubcategoryList();");
    }
    #endregion

    #region 初始化日期控件
    /// <summary>
    /// 初始化日期控件
    /// </summary>
    void InitDateControl()
    {
        new BindControl().BindYearDropList(dropYear, false);
        new BindControl().BindMonthDropList(dropMonth, true);
        dropYear.SelectedValue = DateTime.Now.Year.ToString();
    }
    #endregion

    #region 信息绑定
    void LoadingData()
    {
        string ctrl = "~/admin/finance/_controls/ctlFinanceRecord.ascx";

        StringBuilder SQL = new StringBuilder().Append(" AND DeletionStateCode = 0 AND TypeCode = " + RequestBase.GetFormString("typecode"));

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("category")))
        {
            SQL.Append(" AND CategoryCode = '" + RequestBase.GetFormString("category") + "'");
        }

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("subcategory")))
        {
            SQL.Append(" AND SubcategoryCode= '" + RequestBase.GetFormString("subcategory") + "'");
        }

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("statuscode")))
        {
            SQL.Append(" AND StatusCode= " + RequestBase.GetFormString("statuscode"));
        }

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("month")))
        {
            SQL.Append(" AND DATEDIFF(MONTH, '" + RequestBase.GetFormString("year") + "-" + RequestBase.GetFormString("month") + "-1', EventDate) = 0");
        }
        else
        {
            SQL.Append(" AND DATEDIFF(YEAR, '" + RequestBase.GetFormString("year") + "-1-1', EventDate) = 0");
        }

        string SQL_Sort = "";
        if (!string.IsNullOrEmpty(RequestBase.GetFormString("field")))
        {
            SQL_Sort = " ORDER BY " + RequestBase.GetFormString("field") + " " + RequestBase.GetFormString("level") + " ";
        }
        else
        {
            SQL_Sort = "  ORDER BY EventDate DESC ";
        }

        Control uc_rep = LoadControl(ctrl);

        Repeater rptFinaceRecordList = (Repeater)uc_rep.FindControl("rptFinaceRecordList");

        DBUtility.Pagination pagination = new DBUtility.Pagination();
        try
        {
            if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
            else
                pagination.Sequence = 1;

            pagination.ItemUnit = "条记录";
            pagination.SortSentence = SQL_Sort;
            pagination.Primarykey = "FinanceRecordId";
            pagination.SearchField = "*";
            pagination.TableName = "FinanceRecordBase";
            pagination.PageSize = 30;
            pagination.UrlStr = "";
            pagination.ImagesPath = "../_imgs/grid/";
            pagination.SearchSentence = SQL.ToString();

            rptFinaceRecordList.DataSource = pagination.GetDataTable();
            rptFinaceRecordList.DataBind();

            pagination.PageBarFerrari();
            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptFinaceRecordList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion

    #region 删除信息
    void Delete()
    {
        string res = "";
        res = BLL.LED.FinanceRecordBase.Delete(RequestBase.GetFormString("financerecordid"), false); 
        BLL.AjaxHelper.ResponseWrite(res);
    }
    #endregion
}
